#### TABLES H.8-H.11: FULL MODEL RESULTS WITHOUT COVARIATES
#### Simple treatment effect regressions for all studies

rm(list = ls())
source("./2_code/00_setup.R")
library(texreg)  # Must load after estimatr

#### HELPER FUNCTION ####

extract_models <- function(model_list) {
  output_list <- list()
  for (model in model_list) {
    extracted_model <- extract(model,
                               include.rsquared = FALSE,
                               include.adjrs = FALSE,
                               include.rmse = FALSE,
                               include.ci = FALSE,
                               include.nobs = TRUE)
    output_list <- c(output_list, list(extracted_model))
  }
  return(output_list)
}


#### STUDY 1 ####

data <- fread(paste0(data_path, "data_study1.csv"), header = TRUE)

entry_economic <- lm_robust(exclusion_s ~ treat_economic, data = data[(treat=='economic' | treat=='control')])
entry_humanitarian <- lm_robust(exclusion_s ~ treat_humanitarian, data = data[(treat=='humanitarian' | treat=='control')])
prejudice_economic <- lm_robust(prejudice_s ~ treat_economic, data = data[(treat=='economic' | treat=='control')])
prejudice_humanitarian <- lm_robust(prejudice_s ~ treat_humanitarian, data = data[(treat=='humanitarian' | treat=='control')])


#### STUDY 2 ####

data2 <- fread(paste0(data_path, "data_study2.csv"), header = TRUE)

entry_economic2 <- lm_robust(exclusion_s ~ treat_economic, data = data2[(treat=='economic' | treat=='control')])
entry_humanitarian2 <- lm_robust(exclusion_s ~ treat_humanitarian, data = data2[(treat=='humanitarian' | treat=='control')])
prejudice_economic2 <- lm_robust(prejudice_s ~ treat_economic, data = data2[(treat=='economic' | treat=='control')])
prejudice_humanitarian2 <- lm_robust(prejudice_s ~ treat_humanitarian, data = data2[(treat=='humanitarian' | treat=='control')])


#### STUDY 3, WAVE 1 ####

data3 <- fread(paste0(data_path, "data_study3.csv"), header = TRUE)

entry_economic3 <- lm_robust(exclusion_s ~ treat_economic, data = data3[(treat=='economic' | treat=='control')])
entry_humanitarian3 <- lm_robust(exclusion_s ~ treat_humanitarian, data = data3[(treat=='humanitarian' | treat=='control')])
prejudice_economic3 <- lm_robust(prejudice_s ~ treat_economic, data = data3[(treat=='economic' | treat=='control')])
prejudice_humanitarian3 <- lm_robust(prejudice_s ~ treat_humanitarian, data = data3[(treat=='humanitarian' | treat=='control')])
behavior_economic3 <- lm_robust(quasi_behavior ~ treat_economic, data = data3[(treat=='economic' | treat=='control')])
behavior_humanitarian3 <- lm_robust(quasi_behavior ~ treat_humanitarian, data = data3[(treat=='humanitarian' | treat=='control')])


#### STUDY 3, WAVE 2 ####

data3b <- fread(paste0(data_path, "data_study3_wave2.csv"), header = TRUE)

entry_economic3b <- lm_robust(exclusion_s ~ treat_economic, data = data3b[(treat=='economic' | treat=='control')])
entry_humanitarian3b <- lm_robust(exclusion_s ~ treat_humanitarian, data = data3b[(treat=='humanitarian' | treat=='control')])
prejudice_economic3b <- lm_robust(prejudice_s ~ treat_economic, data = data3b[(treat=='economic' | treat=='control')])
prejudice_humanitarian3b <- lm_robust(prejudice_s ~ treat_humanitarian, data = data3b[(treat=='humanitarian' | treat=='control')])
behavior_economic3b <- lm_robust(quasi_behavior ~ treat_economic, data = data3b[(treat=='economic' | treat=='control')])
behavior_humanitarian3b <- lm_robust(quasi_behavior ~ treat_humanitarian, data = data3b[(treat=='humanitarian' | treat=='control')])


#### EXTRACT MODEL INFO ####

output_list1 <- extract_models(list(entry_economic, entry_humanitarian, 
                                    prejudice_economic, prejudice_humanitarian))

output_list2 <- extract_models(list(entry_economic2, entry_humanitarian2, 
                                    prejudice_economic2, prejudice_humanitarian2))

output_list3 <- extract_models(list(entry_economic3, entry_humanitarian3, 
                                    prejudice_economic3, prejudice_humanitarian3,
                                    behavior_economic3, behavior_humanitarian3))

output_list3b <- extract_models(list(entry_economic3b, entry_humanitarian3b, 
                                     prejudice_economic3b, prejudice_humanitarian3b,
                                     behavior_economic3b, behavior_humanitarian3b))


#### LABELS ####

model_names_1_2 <- c("Open Mig.", "Open Mig.", "Pos. Affect", "Pos. Affect")
model_names_3 <- c("Open Mig.", "Open Mig.", "Pos. Affect", "Pos. Affect", "Behavior", "Behavior")

coeff_names <- list(
  "treat_economic" = "Economic Treatment",
  "treat_humanitarian" = "Humanitarian Treatment",
  "(Intercept)" = "Intercept"
)


#### EXPORT TO LATEX ####

# Table H.8: Study 1
latex_output <- texreg(
  output_list1,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_1_2,
  custom.coef.map = coeff_names,
  caption = "Full Model Results of Study 1 Without Covariates",
  caption.above = TRUE,
  include.ci = FALSE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H8.tex"))

# Table H.9: Study 2
latex_output <- texreg(
  output_list2,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_1_2,
  custom.coef.map = coeff_names,
  caption = "Full Model Results of Study 2 Without Covariates",
  caption.above = TRUE,
  include.ci = FALSE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H9.tex"))

# Table H.10: Study 3 Wave 1
latex_output <- texreg(
  output_list3,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_3,
  custom.coef.map = coeff_names,
  caption = "Full Model Results of Study 3 (Wave 1) Without Covariates",
  caption.above = TRUE,
  include.ci = FALSE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H10.tex"))

# Table H.11: Study 3 Wave 2
latex_output <- texreg(
  output_list3b,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_3,
  custom.coef.map = coeff_names,
  caption = "Full Model Results of Study 3 (Wave 2) Without Covariates",
  caption.above = TRUE,
  include.ci = FALSE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H11.tex"))

